package baratopradedeu

import groovy.sql.Sql

class OfertaController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    
	def defaultAction = 'list'

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [ofertaInstanceList: Oferta.list(params), ofertaInstanceTotal: Oferta.count()]
    }

    def create = {
        def ofertaInstance = new Oferta()
        ofertaInstance.properties = params
        return [ofertaInstance: ofertaInstance]
    }

    def save = {
        def ofertaInstance = new Oferta(params)
        if (ofertaInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'oferta.label', default: 'Oferta'), ofertaInstance.id])}"
            redirect(action: "show", id: ofertaInstance.id)
        }
        else {
            render(view: "create", model: [ofertaInstance: ofertaInstance])
        }
    }

    def show = {
        def ofertaInstance = Oferta.get(params.id)
        if (!ofertaInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'oferta.label', default: 'Oferta'), params.id])}"
            redirect(action: "list")
        }
        else {
            [ofertaInstance: ofertaInstance]
        }
    }

    def edit = {
        def ofertaInstance = Oferta.get(params.id)
        if (!ofertaInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'oferta.label', default: 'Oferta'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [ofertaInstance: ofertaInstance]
        }
    }

    def update = {
        def ofertaInstance = Oferta.get(params.id)
        if (ofertaInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (ofertaInstance.version > version) {
                    
                    ofertaInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'oferta.label', default: 'Oferta')] as Object[], "Another user has updated this Oferta while you were editing")
                    render(view: "edit", model: [ofertaInstance: ofertaInstance])
                    return
                }
            }
            ofertaInstance.properties = params
            if (!ofertaInstance.hasErrors() && ofertaInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'oferta.label', default: 'Oferta'), ofertaInstance.id])}"
                redirect(action: "show", id: ofertaInstance.id)
            }
            else {
                render(view: "edit", model: [ofertaInstance: ofertaInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'oferta.label', default: 'Oferta'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def ofertaInstance = Oferta.get(params.id)
        if (ofertaInstance) {
            try {
                ofertaInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'oferta.label', default: 'Oferta'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'oferta.label', default: 'Oferta'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'oferta.label', default: 'Oferta'), params.id])}"
            redirect(action: "list")
        }
    }
	
	def comprar = {
		if(session.cliente){
		    [oferta:ofertaId]
			redirect(controller: "pedido", action: "comprar")
		}else{
			redirect(controller: "cliente", action: "login")
		}
    }
    
    def dataSource
    
    def ofertaMaisVendida = {
     
    	def db = new Sql(dataSource) 

        def result = db.rows("select oferta_id,count(oferta_id) from Pedido group by oferta_id limit 1") 
     	
     	Long id = result.oferta_id.get(0)    	
     	 
        [ofertaInstanceList: Oferta.findById(id)]
     }
}
